package com.czkt.shop.controller;

import com.czkt.shop.pojo.GoodsCategory;
import com.czkt.shop.service.IGoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2024-09-04
 */
@RestController
@RequestMapping("/goods/category")
@CrossOrigin
public class GoodsCategoryController {

    @Autowired
    IGoodsCategoryService goodsCategoryService;

    @RequestMapping("/list")
    public Object list(){
        return goodsCategoryService.list();
    }

    @RequestMapping("/parentId")
    public Object getParent(Integer pid){

        List<GoodsCategory> readList = goodsCategoryService.list(pid);

        Set<Integer> set = readList.stream().map(item->{
            return item.getId();
        }).collect(Collectors.toSet());

        System.out.println(set);

        List<GoodsCategory> tireList = goodsCategoryService.list(set);

        Map<String,Object> map = new HashMap<>();

        map.put("secondList",readList);
        map.put("tireList",tireList);

        return map;
    }

}
